package BOH.GUI;
import BOH.data.EateryDBConnection;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * The Back of House System for a
 * small restaurant.
 * 
 * Maeghan Colvin & Jennifer Haveman
 * CSC 289 0B01
 * February 13, 2014
 */

public class SearchEmployeePanel extends JPanel
{
    // Variables
    private JFrame frame;
    private JPanel mainPanel;               // Holds the mainPanel
    private JPanel employeeSearchPanel;     // Holds the employeeSearchPanel
    private JPanel addEditPanel;            // Holds addEditPanel
    private JList employeeSearchList;       // Holds existing employee names
    private JScrollPane scrollPane;         // Adds a scroll pane
    private JComboBox addEditBox;           // Holds the add/edit options
    private JButton addEditEmployeeButton;  // Add/edit button
    private String[] employeesArray;        // Holds employees names for JList
    private String[] employeeInformationArray;//Holds selected employee information
    private int index = 0;                  // Index for JList Selection
    private String[] addEditArray = 
    {"Add", "Edit", "Remove"};              // Used for combobox
    private String comboBoxSelection = "100";// Holds the combo box selection
    private String sqlStatement;            // Searchs for employee names
    private String sqlStatement2;           // Searches for specific employee
    
    /**
     * Constructor
     */
    
    public SearchEmployeePanel()
    {
        // Set Look and Feel.
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        // Create and set up the window.
        frame = new JFrame("I Don't Know Eatery");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Build and add mainPanel
        this.buildMainPanel();
        frame.add(mainPanel);
        
        // Add content to the window.
        frame.setSize(300, 300);
        frame.validate();
        
        // Display the window.
        frame.setVisible(true);  
    }
    
    /**
     * The buildMainPanel builds the 
     * mainPanel.
     */
    
    private void buildMainPanel() 
    {
        // Create mainPanel
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Call list JPanel
        buildEmployeeSearchPanel();
        
        // Call combobox and button JPanel
        buildAddEditPanel();
        
        // Add panels to mainPanel
        mainPanel.add(employeeSearchPanel, BorderLayout.CENTER);
        mainPanel.add(addEditPanel, BorderLayout.SOUTH);
    }
    
    /**
     * The buildEmployeeSearchPanel builds a list box
     * filled with current employees first and last name.
     * The manager can select a name and edit or add a new 
     * employee.
     */
    
    private JPanel buildEmployeeSearchPanel()
    {
        // Create employeeSearchPanel
        employeeSearchPanel = new JPanel(new BorderLayout());
        employeeSearchPanel.setBorder(BorderFactory.createTitledBorder("Select Employee:"));
        
        // Get array to fill the JList
        employeesArray = getResults();
        
        // Create JList and add components
        employeeSearchList = new JList(employeesArray);
        employeeSearchList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        employeeSearchList.setFixedCellWidth(125);
        employeeSearchList.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        employeeSearchList.addListSelectionListener(new ListListener());
        employeeSearchList.setToolTipText("Select an employee");    // Set the tool tip
        scrollPane = new JScrollPane(employeeSearchList);
        employeeSearchPanel.add(scrollPane);
        
        // Return
        return employeeSearchPanel;
    }
    
    /**
     * The buildEmployeeSearchPanel builds a list box
     * filled with current employees first and last name.
     * The manager can select a name and edit or add a new 
     * employee.
     */
    
    private JPanel buildAddEditPanel()
    {
        // Create new panel
        addEditPanel = new JPanel(new BorderLayout());
        addEditPanel.setBorder(BorderFactory.createEmptyBorder());
        
        // Create combo box
        addEditBox = new JComboBox(addEditArray);
        addEditBox.setToolTipText("Add/edit/Remove employee");
        addEditBox.addActionListener(new ComboBoxListener());
        addEditPanel.add(addEditBox, BorderLayout.CENTER);
        
        // Create the JButton
        addEditEmployeeButton = new JButton("Update");
        addEditEmployeeButton.setToolTipText("Update employee information");
        addEditEmployeeButton.setMnemonic(KeyEvent.VK_U);
        addEditEmployeeButton.addActionListener(new addEditButtonListener());
        addEditPanel.add(addEditEmployeeButton, BorderLayout.SOUTH);
        
        // Return
        return addEditPanel;
    }
    
    /**
     * The databaseSearch determines
     * the information for the JList.
     * @param empNumber
     * @return
     */
    public String databaseSearch()
    {
        sqlStatement = "SELECT LastName || ', ' || FirstName " +
                       "FROM Employee";
        
        // Return
        return sqlStatement;
    }
    
    /**
     * The employeeSearch determines
     * the selected employee.
     * @return
     */
    public String employeeSearch()
    {
        index += 100;
        sqlStatement2 = "SELECT * " +
                        "FROM Employee " +
                        "WHERE employeeID = " + index;
        
        // Return
        return sqlStatement2;
    }

    /**
     * The getResults method gets the names
     * to fill the JList.
     * @return
     */
    
    public String[] getResults()
    {
        sqlStatement = databaseSearch();
        employeesArray = EateryDBConnection.getData(sqlStatement);
        
        // Return
        return employeesArray;
    }
    
    /**
     * The getEmployeeResults method gets selected     
     * employee to fill the editForm.
     * @param e 
     */
    
    public String[] getEmployeeResults()
    {
        sqlStatement2 = employeeSearch();
        employeeInformationArray = EateryDBConnection.getData(sqlStatement2);
        
        // Return
        return employeeInformationArray;
    }
    
    /**
     * The getBoxSelection method returns
     * the value in the comboBox
     * @param e 
     */
    
    public String getBoxSelection()
    {
        // Get the selected option
        comboBoxSelection = (String) addEditBox.getSelectedItem();
        return comboBoxSelection;
    }
    
    /*
     * The ComboBoxListener is used to indicate the selected value
     * when an item in the ComboBox is selected.
     */
    
    private class ComboBoxListener implements ActionListener 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
           comboBoxSelection = getBoxSelection();
        }
    }
    
     /*
     * The ListListener is used to indicate the selected i
     * when an item in the ListBox is clicked.
     */
    
    private class ListListener implements ListSelectionListener 
    {
        @Override
        public void valueChanged(ListSelectionEvent e) 
        {
            if (!e.getValueIsAdjusting()) 
            {
                // Find index of employee selected in listbox
               index = employeeSearchList.getSelectedIndex();
            }
        }
    }
    
    /*
     * The addEditButtonListener is used to indicate the selected value
     * when an item in the ComboBox is selected.
     */
    
    private class addEditButtonListener implements ActionListener 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // Get comboBoxSelection
            comboBoxSelection = getBoxSelection();
            
            // Get employee information
            employeeInformationArray = getEmployeeResults();
            
            // Determine proper screen to display
            switch (comboBoxSelection) 
            {
                case "Add":
                    // Open the add employee screen
                    AddEditRemoveEmployeePanel addEmployee = new AddEditRemoveEmployeePanel(employeeInformationArray, comboBoxSelection);
                    frame.setVisible(false);
                    break;
                case "Edit":
                    // Open the edit employee screen
                    addEmployee = new AddEditRemoveEmployeePanel(employeeInformationArray, comboBoxSelection);
                    frame.setVisible(false);
                    break;
                case "Remove":
                    // Open the remove employee screen
                    addEmployee = new AddEditRemoveEmployeePanel(employeeInformationArray, comboBoxSelection);
                    frame.setVisible(false);
                    break;
                
            }
        }
    }
}
